package com.github.cakin.shiro.chapter3.permission;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.apache.shiro.authz.permission.WildcardPermission;

import java.util.Arrays;
import java.util.Collection;

/**
 * @className: MyRolePermissionResolver
 * @description: 用于根据角色字符串来解析得到权限集合。
 * @date: 2020/5/17
 * @author: cakin
 */
public class MyRolePermissionResolver implements RolePermissionResolver {
    /**
     * 功能描述：如果用户拥有 role1，那么就返回一个 “menu:*” 的权限，也就是role1 绑定了 menu:* 权限
     *
     * @author cakin
     * @date 2020/5/17
     * @param roleString 角色字符串
     * @return Collection<Permission> 权限集合
     * @description 触发该函数的调用栈
     * resolvePermissionsInRole:26, MyRolePermissionResolver (com.github.cakin.shiro.chapter3.permission)
     * resolveRolePermissions:446, AuthorizingRealm (org.apache.shiro.realm)
     * getPermissions:414, AuthorizingRealm (org.apache.shiro.realm)
     * isPermitted:466, AuthorizingRealm (org.apache.shiro.realm)
     * isPermitted:462, AuthorizingRealm (org.apache.shiro.realm)
     * isPermitted:457, AuthorizingRealm (org.apache.shiro.realm)
     * isPermitted:223, ModularRealmAuthorizer (org.apache.shiro.authz)
     * isPermitted:113, AuthorizingSecurityManager (org.apache.shiro.mgt)
     * isPermitted:158, DelegatingSubject (org.apache.shiro.subject.support)
     * testIsPermitted:52, AuthorizerTest (com.github.cakin.shiro.chapter3)
     */
    public Collection<Permission> resolvePermissionsInRole( String roleString ) {
        if ("role1".equals(roleString)) {
            return Arrays.asList((Permission) new WildcardPermission("menu:*"));
        }
        return null;
    }
}
